// https://www.programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
// https://leetcode.cn/problems/reverse-linked-list/description/

// 双指针
const reverseList = function (head) {
  if (!head || !head.next) return head;
  let pre = null,
    cur = head;
  while (cur) {
    // 因为要修改节点的指向 所以要提前保存一下当前节点的下一个节点指向
    let temp = cur.next;
    cur.next = pre;
    pre = cur;
    cur = temp;
  }
  return pre;
};

// 递归
const reverseList1 = function (head) {
  if (head == null || head.next == null) {
    return head;
  }
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
};
